package com.example.yls.demoa;

import android.app.Activity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private static Activity mActivity;
    // 底部菜单两个Linearlayout
    private LinearLayout ll_home;
    private LinearLayout ll_friend;
    // 底部菜单两个个ImageView
    private ImageView iv_home;
    private ImageView iv_friend;

    // 底部菜单两个个菜单标题
    private TextView tv_home;
    private TextView tv_friend;
    // 两个Fragment
    private Fragment writeFragment;
    private Fragment friendFragment;





    public static Activity getActivity() {
        return mActivity;
    }

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initToolBar();
        mActivity = MainActivity.this;
        // 初始化控件
        initView();
        // 初始化底部按钮事件
        initEvent();
        // 初始化并设置当前Fragment
        initFragment(0);
    }

    private void initToolBar() {
        Toolbar toolbar=findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);


    }

    private void initEvent() {
        // 设置按钮监听
        ll_home.setOnClickListener(this);
        ll_friend.setOnClickListener(this);

    }
    public void onClick(View v) {
        // 在每次点击后将所有的底部按钮(ImageView,TextView)颜色改为灰色，然后根据点击着色
        restartBotton();
        // ImageView和TetxView置为绿色，页面随之跳转
        switch (v.getId()) {
            case R.id.ll_home:
                //   iv_home.setImageResource(R.drawable.tab_weixin_pressed);
                tv_home.setTextColor(0xff1B940A);
                initFragment(0);
                break;

            case R.id.ll_friend:
                //   iv_friend.setImageResource(R.drawable.tab_find_frd_pressed);
                tv_friend.setTextColor(0xff1B940A);
                initFragment(2);
                break;

            default:
                break;
        }

    }


    private void initView() {
        // 底部菜单4个Linearlayout
        this.ll_home = (LinearLayout) findViewById(R.id.ll_home);
        this.ll_friend = (LinearLayout) findViewById(R.id.ll_friend);


        // 底部菜单4个ImageView
        this.iv_home = (ImageView) findViewById(R.id.iv_home);
        this.iv_friend = (ImageView) findViewById(R.id.iv_friend);

        // 底部菜单4个菜单标题
        this.tv_home = (TextView) findViewById(R.id.tv_home);
        this.tv_friend = (TextView) findViewById(R.id.tv_friend);


    }
    private void initFragment(int i) {
        // 由于是引用了V4包下的Fragment，所以这里的管理器要用getSupportFragmentManager获取
        FragmentManager fragmentManager = getSupportFragmentManager();
        // 开启事务
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        // 隐藏所有Fragment
        hideFragment(transaction);
        switch (i) {
            case 0:
                if (writeFragment == null) {
                    writeFragment = new WriteFragment();
                    transaction.add(R.id.fl_content, writeFragment);
                } else {
                    transaction.show(writeFragment);
                }
                break;
            case 2:
                friendFragment = new FriendFragment();
                transaction.add(R.id.fl_content, friendFragment);

              /*  if (friendFragment == null) {
                    friendFragment = new FriendFragment();
                    transaction.add(R.id.fl_content, friendFragment);
                } else {
                    transaction.show(friendFragment);
                }*/

                break;

            default:
                break;
        }
        transaction.commit();
    }
    private void hideFragment(FragmentTransaction transaction) {
        if (writeFragment != null) {
            transaction.hide(writeFragment);
        }

        if (friendFragment != null) {
            transaction.hide(friendFragment);
        }
    }

    private void restartBotton() {
        // ImageView置为灰色
        //   iv_home.setImageResource(R.drawable.tab_weixin_normal);
        //      iv_friend.setImageResource(R.drawable.tab_find_frd_normal);
        // TextView置为白色
        tv_home.setTextColor(0xffffffff);
        tv_friend.setTextColor(0xffffffff);
    }
}

